package com.mxwl.cbg.common.Extract;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class LingShiExtract extends BaseExtract {
    private static final Pattern enhanceLevelPattern = Pattern.compile("精炼等级[\\s:：]*?(\\d+)");
    private static final Pattern attributePattern = Pattern.compile("\\b(\\p{IsHan}+?)\\s*([+-]\\s*[\\d.]+)\\b");

    public static int extractEnhanceLevel(String text) {
        // 正则表达式匹配锻炼等级

        String[] lines = text.split("\\r?\\n");
        for (String line : lines) {
            if (line.contains("精炼等级")) {
                Matcher matcher = enhanceLevelPattern.matcher(line);
                if (matcher.find()) {
                    // 提取数字部分并转为整数
                    return Integer.parseInt(matcher.group(1));
                }
            }
        }

        return 0; // 默认值，表示未找到锻炼等级
    }

    public static List<Attribute> extractAttributes(String text) {
        List<Attribute> attributes = new ArrayList<>();
        // 按行处理文本
        String[] lines = text.split("\\r?\\n");
        for (String line : lines) {
            // 跳过符石行和星相互合行
            if (line.contains("符石") || line.contains("星相互合") || line.contains("星位")) {
                continue;
            }
            Matcher matcher = attributePattern.matcher(line);
            while (matcher.find()) {
                String attributeName = matcher.group(1);
                String attributeValue = matcher.group(2);
                Attribute attribute = new Attribute(attributeName, Double.valueOf(attributeValue));
                attributes.add(attribute);
            }
        }
        return attributes;
    }

    public record Attribute(String name, Double value) {
    }
}
